package com.spt.springbootzhsx.service;

import com.spt.springbootzhsx.dao.OrderDao;
import com.spt.springbootzhsx.entity.User;
import com.spt.springbootzhsx.entity.Vip;
import com.spt.springbootzhsx.entity.back.UserBack;
import com.spt.springbootzhsx.entity.count.UserWatchRecord;
import com.spt.springbootzhsx.mapper.UserMapper;
import com.spt.springbootzhsx.mapper.VipMapper;
import com.spt.springbootzhsx.redis.RedisOp;
import com.spt.springbootzhsx.utils.DateUtil;
import com.spt.springbootzhsx.utils.MailUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.SimpleBeanInfo;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class UserImpl implements UserService{
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private VipMapper vipMapper;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private RedisOp redisOp;

    private int yzm;

    @Override
    public User getUserByUid(Integer uid) {
        User user=userMapper.getUserByUid(uid);
        user.setAddTime(DateUtil.formatDate(user.getAddTime()));
        return user;
    }

    @Override
    public Integer registUser(User user) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        Date date=new Date();
        String time=simpleDateFormat.format(date);
        try {
            user.setAddTime(simpleDateFormat.parse(time));
        } catch (ParseException e) {
            System.out.println("添加时间获取异常！");
            e.printStackTrace();
        }
        int k=userMapper.addUser(user);
        if(k==1){
            User user1=userMapper.getUserByEmail(user.getEmail());
            userMapper.updateHeadSrc(user1.getUid(),"默认.jpg");
            return user1.getUid();
        }else{
            return -1;
        }
    }

    @Override
    public User updateUser(User user) {
        int k=userMapper.updateUser(user);
        if(k>=1){
            return userMapper.getUserByUid(user.getUid());
        }else{
            return null;
        }
    }

    @Override
    public User vipPay(Integer uid) {
        try{
            User user = userMapper.getUserByUid(uid);

            if(user.getVid() == null){
                Date d1 = new Date();
                Calendar calendar = new GregorianCalendar();
                calendar.setTime(d1);
                calendar.add(calendar.DATE,30);
                Date d2 = calendar.getTime();

                vipMapper.addVip(new Vip(null,d1,d2,"未过期",uid));

                user.setVid(uid);
                userMapper.updateUser(user);
                return userMapper.getUserByUid(uid);
            }else{
                Date d1 = new Date();
                Calendar calendar = new GregorianCalendar();
                calendar.setTime(d1);
                calendar.add(calendar.DATE,30);
                Date d2 = calendar.getTime();

                Vip vip = new Vip(null,d1,d2,"未过期",uid);
                vipMapper.updateVip(vip);
                return userMapper.getUserByUid(uid);
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
            return null;
        }
    }

    @Override
    public String findPassword_step1(Integer uid) {
        try {
            //String mail="1661892664@qq.com";
            String mail = userMapper.getUserByUid(uid).getEmail();
            //使用随机数生成简易的验证码
            double ma=Math.random()*10000;
            this.yzm = (int) ma;
            //sService.updateYzm(ma1,sid);

            MailUtils.sendMail(mail,yzm); //收件人邮箱和验证码
            System.out.println(this.yzm);

            return "OK";
        }catch (Exception e){
            System.out.println(e.getMessage());
            return null;
        }
    }

    @Override
    public String findPassword_step2(Integer uid, String password, Integer yzm) {
        try {
            User user = userMapper.getUserByUid(uid);

            if(this.yzm == yzm){
                user.setPassword(password);
                userMapper.updateUser(user);
                System.out.println("*********密码修改成功！*********");
                this.yzm = -1;

                return "OK";
            }else {
                System.out.println("************error************");
                return null;
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
            return null;
        }
    }

    @Override
    public ArrayList<UserWatchRecord> getRecordByUid(Integer uid) {
        ArrayList<UserWatchRecord> list=new ArrayList<>();
        UserWatchRecord ur1=new UserWatchRecord();
        UserWatchRecord ur2=new UserWatchRecord();
        UserWatchRecord ur3=new UserWatchRecord();
        UserWatchRecord ur4=new UserWatchRecord();
        UserWatchRecord ur5=new UserWatchRecord();
        UserWatchRecord ur6=new UserWatchRecord();
        UserWatchRecord ur7=new UserWatchRecord();
        UserWatchRecord ur8=new UserWatchRecord();

        ur1.setType("剧情");
        ur2.setType("家庭");
        ur3.setType("动作");
        ur4.setType("冒险");
        ur5.setType("科幻");
        ur6.setType("爱情");
        ur7.setType("历史");
        ur8.setType("战争");

        ur1.setCount(orderDao.getOrderCountByUidAndMtype(uid,ur1.getType()));
        ur2.setCount(orderDao.getOrderCountByUidAndMtype(uid,ur2.getType()));
        ur3.setCount(orderDao.getOrderCountByUidAndMtype(uid,ur3.getType()));
        ur4.setCount(orderDao.getOrderCountByUidAndMtype(uid,ur4.getType()));
        ur5.setCount(orderDao.getOrderCountByUidAndMtype(uid,ur5.getType()));
        ur6.setCount(orderDao.getOrderCountByUidAndMtype(uid,ur6.getType()));
        ur7.setCount(orderDao.getOrderCountByUidAndMtype(uid,ur7.getType()));
        ur8.setCount(orderDao.getOrderCountByUidAndMtype(uid,ur8.getType()));

        list.add(ur1);
        list.add(ur2);
        list.add(ur3);
        list.add(ur4);
        list.add(ur5);
        list.add(ur6);
        list.add(ur7);
        list.add(ur8);

        list.sort(new Comparator<UserWatchRecord>() {
            @Override
            public int compare(UserWatchRecord o1, UserWatchRecord o2) {
                return o2.getCount()-o1.getCount();
            }
        });
        return list;
    }

    @Override
    public ArrayList<UserBack> getUserAndVip() {
        ArrayList<UserBack> list=new ArrayList<>();
        ArrayList<User> ulist=userMapper.getAllUser();
        for (User user : ulist) {
            UserBack userBack=new UserBack();
            userBack.setUid(user.getUid());
            userBack.setPassword(user.getPassword());
            userBack.setUsername(user.getUsername());
            userBack.setAddTime(DateUtil.formatDate(user.getAddTime()));
            userBack.setEmail(user.getEmail());
            userBack.setGender(user.getGender());
            userBack.setPhone(user.getPhone());
            userBack.setHeadsrc(user.getHeadsrc());
            if(user.getVid()!=null){
                Vip vip=vipMapper.getVipByVidUseful(user.getVid());
                if(vip!=null){
                    userBack.setStartTime(vip.getStartTime());
                    userBack.setEndTime(vip.getEndTime());
                    userBack.setVstate(vip.getVstate());
                }
            }
            list.add(userBack);
        }
        return list;
    }

    @Override
    public String deleteUserAndVip(Integer uid) {
        User user=userMapper.getUserByUid(uid);
        if(user!=null){
            user.setVid(null);
            userMapper.updateUser(user);
            ArrayList<Vip> list=vipMapper.getVipByUid(uid);
            if(list!=null){
               vipMapper.deleteVipByUid(uid);
            }
            int k=userMapper.deleteUser(uid);
            if(k>=1){
                return "删除成功！";
            }else{
                return "删除失败！";
            }
        }
        return "删除失败！";
    }

    @Override
    public String getOpenIdByToken(String token) {
        String[] res=redisOp.get("token"+token).split(";");
        if(res.length!=3) return "token错误！";
        return res[0];
    }

    @Override
    public String getSessionKeyByToken(String token) {
        String[] res=redisOp.get("token"+token).split(";");
        if(res.length!=3) return "token错误！";
        return res[1];
    }

    @Override
    public String getUnionIdByToken(String token) {
        String[] res=redisOp.get("token"+token).split(";");
        if(res.length!=3) return "token错误！";
        return res[2];
    }
}
